//
//  LeftViewController.m
//  IFengNews
//
//  Created by lanou3g on 16/3/2.
//  Copyright © 2016年 徐强. All rights reserved.
//

#import "LeftViewController.h"
#import "HeaderLineViewController.h"
#import "EntertainmentViewController.h"
#import "SportsViewController.h"
#import "STViewController.h"
#import "FEViewController.h"
#import "SocityViewController.h"
#import "LoginViewController.h"
#import "VideoViewController.h"
#import "PicViewController.h"
#import "AppDelegate.h"
@interface LeftViewController ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic,strong)UIImageView *imV;
@property(nonatomic,strong)UITableView *tableView;
@property(nonatomic,strong)NSMutableArray *arrAllData;
@property(nonatomic,strong)HeaderLineViewController *headerV;
@property(nonatomic,strong)EntertainmentViewController *enterV;
@property(nonatomic,strong)SportsViewController *sportsV;
@property(nonatomic,strong)STViewController *STV;
@property(nonatomic,strong)FEViewController *FEV;
@property(nonatomic,strong)SocityViewController *socityV;
@property(nonatomic,strong)LoginViewController *loginV;
@property(nonatomic,strong)PicViewController *picFallV;
@property(nonatomic,strong)VideoViewController *videoVC;
@end

@implementation LeftViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    [self initView];
    
}
- (void)viewWillAppear:(BOOL)animated{
    if (self.loginV.isOnLine) {
        self.imV.image = [UIImage imageNamed:@"头像"];
    }else{
        self.imV.image = [UIImage imageNamed:@"comment_user_icon"];
    }
}
- (void)initView{
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.view.backgroundColor = [UIColor grayColor];
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"UITableViewCell_Left"];
    [self.view addSubview:self.tableView];
    self.imV.layer.cornerRadius = kScreenWidth * 0.1;
    self.imV.layer.masksToBounds = YES;
    [self.view addSubview:self.imV];
}
//UITableViewDataSource,UITableViewDelegate
- (NSInteger )tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.arrAllData.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell_Left" forIndexPath:indexPath];
    cell.textLabel.text = self.arrAllData[indexPath.row];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.1;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    AppDelegate *app = [UIApplication sharedApplication].delegate;
    if ([cell.textLabel.text isEqualToString:@"首页"]) {
        UINavigationController *header_navi = [[UINavigationController alloc]initWithRootViewController:self.headerV];
        [app.rootVC setRootViewController:header_navi];
    }else if ([cell.textLabel.text isEqualToString:@"娱乐"]){
        UINavigationController *enter_navi = [[UINavigationController alloc]initWithRootViewController:self.enterV];
        [app.rootVC setRootViewController:enter_navi];
    }else if ([cell.textLabel.text isEqualToString:@"体育"]){
        UINavigationController *sport_navi = [[UINavigationController alloc]initWithRootViewController:self.sportsV];
        [app.rootVC setRootViewController:sport_navi];
    }else if ([cell.textLabel.text isEqualToString:@"科技"]){
        UINavigationController *enter_navi = [[UINavigationController alloc]initWithRootViewController:self.STV];
        [app.rootVC setRootViewController:enter_navi];
    }else if ([cell.textLabel.text isEqualToString:@"财经"]){
        UINavigationController *enter_navi = [[UINavigationController alloc]initWithRootViewController:self.FEV];
        [app.rootVC setRootViewController:enter_navi];
    }else if ([cell.textLabel.text isEqualToString:@"社会"]){
        UINavigationController *enter_navi = [[UINavigationController alloc]initWithRootViewController:self.socityV];
        [app.rootVC setRootViewController:enter_navi];
    }else if ([cell.textLabel.text isEqualToString:@"图片"]){
        UINavigationController *pic_navi = [[UINavigationController alloc]initWithRootViewController:self.picFallV];
        [app.rootVC setRootViewController:pic_navi];
    }else if ([cell.textLabel.text isEqualToString:@"视频"]){
        UINavigationController *video_navi = [[UINavigationController alloc]initWithRootViewController:self.videoVC];
        [app.rootVC setRootViewController:video_navi];
    }
}
//头像的点击事件
- (void)tapAction{
    if (self.loginV.isOnLine) {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"是否注销" preferredStyle:(UIAlertControllerStyleAlert)];
        __weak typeof(self) weakSelf = self;
        UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleCancel) handler:^(UIAlertAction * _Nonnull action) {
            //取消注销
            [weakSelf dismissViewControllerAnimated:YES completion:nil];
        }];
        UIAlertAction *ensure = [UIAlertAction actionWithTitle:@"确认" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
            weakSelf.loginV.onLine = NO;
             weakSelf.imV.image = [UIImage imageNamed:@"comment_user_icon"];
        }];
        [alert addAction:ensure];
        [alert addAction:cancel];
        [self presentViewController:alert animated:YES completion:nil];
    }else
    {
        [self presentViewController:self.loginV animated:YES completion:nil];
    }
}
#pragma mark - 懒加载
- (VideoViewController *)videoVC{
    if (!_videoVC) {
        _videoVC = [VideoViewController new];
    }
    return _videoVC;
}
- (PicViewController *)picFallV{
    if (!_picFallV) {
        _picFallV = [PicViewController new];
    }
    return _picFallV;
}
- (LoginViewController *)loginV{
    if (!_loginV) {
        _loginV = [LoginViewController new];
    }
    return _loginV;
}
- (SocityViewController *)socityV{
    if (!_socityV) {
        _socityV = [SocityViewController new];
    }
    return _socityV;
}
- (FEViewController *)FEV{
    if (!_FEV) {
        _FEV = [FEViewController new];
    }
    return _FEV;
}
- (STViewController *)STV{
    if (!_STV) {
        _STV = [STViewController new];
    }
    return _STV;
}
- (SportsViewController *)sportsV{
    if (!_sportsV) {
        _sportsV = [SportsViewController new];
    }
    return _sportsV;
}
- (EntertainmentViewController *)enterV{
    if (!_enterV) {
        _enterV = [EntertainmentViewController new];
    }
    return _enterV;
}
- (HeaderLineViewController *)headerV{
    if (!_headerV) {
        _headerV = [HeaderLineViewController new];
    }
    return _headerV;
}
- (UIImageView *)imV{
    if (!_imV) {
        _imV = [[UIImageView alloc]initWithFrame:CGRectMake(kScreenWidth * 0.3, kScreenHeight * 0.1, kScreenWidth * 0.2, kScreenWidth * 0.2)];
        _imV.image = [UIImage imageNamed:@"comment_user_icon"];
        _imV.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction)];
        [_imV addGestureRecognizer:tap];
    }
    return _imV;
}
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, kScreenHeight * 0.2 + kScreenWidth * 0.2, kScreenWidth * 4.0 / 5, kScreenHeight * 0.8 - kScreenWidth * 0.2) style:(UITableViewStylePlain)];
    }
    return _tableView;
}
- (NSMutableArray *)arrAllData{
    if (!_arrAllData) {
        _arrAllData = [NSMutableArray arrayWithObjects:@"首页",@"娱乐",@"体育",@"科技",@"财经",@"社会",@"图片",@"视频", nil];
    }
    return _arrAllData;
}
@end
